//
//  PictureInFlickr.m (aka PictureViewController)
//
//  Created by Viswanadh Nekkanti on 02/07/2012.
//  Copyright (c) 2012 UGuideUs, for SFU Pathways.
//  Software is licensed under GNU GPL.
//

#import "PictureInFlickr.h"


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Private interface definitions
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@interface PictureInFlickr(private)<UIScrollViewDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)slideViewOffScreen;
- (void)searchFlickrPhotos:(NSString *)text;
@end

//#error 
// Replace this with your Flickr key
NSString *const FlickrAPIKey = @"1724659a4df8ae1557131d522ad92b2b";

// Replace this with your Flickr the user who's photo to search 
NSString *const NSID=@"82719489@N08";

@implementation PictureInFlickr

@synthesize picturename = _picturename;
@synthesize imageInFlickr=_imageInFlickr;
@synthesize imageScroll = _imageScroll;

- (void)setPicturename : (NSString *)picturenameTobeSearched
{
    //NSLog(@"%@",picturename);	
    _picturename = picturenameTobeSearched;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    
    if (_picturename) 
    {
        self.imageScroll.delegate = self;
        NSLog(@"pictureName=%@",_picturename);
        [self searchFlickrPhotos];
    }
}

- (void)viewDidUnload 
{
    
    [self setImageInFlickr:nil];
    [self setImageInFlickr:nil];
    [self setImageScroll:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}


/*-------------------------------------------------------------
 *
 *------------------------------------------------------------*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    // Store incoming data into a string
	NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    //    NSLog(@"CALLING:%@", jsonString);  
    
    // Create a dictionary from the JSON string
	NSDictionary *JASONResults = [jsonString JSONValue];
	
    // Build an array from the dictionary for easy access to each entry
	NSArray *photos = [[JASONResults objectForKey:@"photos"] objectForKey:@"photo"];
    
    // Loop through each entry in the dictionary...
	for (NSDictionary *photo in photos)
    {
        
        // Build and save the URL to the large image so we can zoom
        // in on the image if requested
		NSString *photoURLString = [NSString stringWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_b.jpg", [photo objectForKey:@"farm"], [photo objectForKey:@"server"], [photo objectForKey:@"id"], [photo objectForKey:@"secret"]];
        NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:photoURLString]];
        NSLog(@"The image size=%d",[imageData length]);
        
        UIImage *theImage = [UIImage imageWithData:imageData];
        [_imageInFlickr setImage:theImage];
        
        self.imageScroll.contentSize = self.imageInFlickr.image.size;
	}
    
}

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)imageScroll
{
    // return which subview we want to zoom
    self.imageScroll.contentSize =self.imageInFlickr.image.size;
    return self.imageInFlickr;
}


-(void)searchFlickrPhotos
{
    // Build the string to call the Flickr API
	NSString *urlString = [NSString stringWithFormat:@"http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%@&user_id=%@&tags=%@&per_page=15&format=json&nojsoncallback=1", FlickrAPIKey, NSID,self.picturename];
    
    // Create NSURL string from formatted string, by calling the Flickr API
	NSURL *url = [NSURL URLWithString:urlString];
    
    //  NSLog(@"%@", url);  
    //  
    
    //  Setup and start async download
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if(connection){
        
    }
}


@end
